<template>
  <el-card>
    <div slot="header">
      <span>我的填报单</span>
    </div>
    <el-row>
      <el-form :inline="true" :model="queryParam">
        <el-form-item label="标题">
          <el-input
            v-model="queryParam.QueryKey"
            placeholder="标题"
            @change="load"
          />
        </el-form-item>
        <el-form-item label="填报类型">
          <treeDicItem
            v-model="queryParam.FillType"
            :dic-id="DictItemDic.fillInType"
            placeholder="填报类型"
            @load="loadFillType"
            @change="load"
          />
        </el-form-item>
        <el-form-item label="填报状态">
          <enumItem
            v-model="queryParam.Status"
            :dic-key="EnumDic.EmpFillInStatus"
            placeholder="填报状态"
            sys-head="oa"
            @change="load"
          />
        </el-form-item>
        <el-form-item label="开始日期">
          <el-date-picker
            v-model="queryParam.Begin"
            placeholder="开始日期"
            @change="load"
          />
        </el-form-item>
        <el-form-item label="截止日期">
          <el-date-picker
            v-model="queryParam.End"
            placeholder="截止日期"
            @change="load"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <w-table
      :data="items"
      :columns="columns"
      :is-paging="true"
      row-key="Id"
      :paging="paging"
      @load="load"
    >
      <template slot="fillType" slot-scope="e">
        <span v-if="fillType && fillType[e.row.FillType]">{{
          fillType[e.row.FillType].DicText
        }}</span>
      </template>
      <template slot="fillInTime" slot-scope="e">
        {{
          moment(e.row.BeginDate).format("YYYY-MM-DD") +
            " 至 " +
            moment(e.row.EndDate).format("YYYY-MM-DD")
        }}
      </template>
      <template slot="fillTime" slot-scope="e">
        <span v-if="e.row.Status == 1">
          {{ moment(e.row.FillInTime).format("YYYY-MM-DD HH:mm") }}</span>
      </template>
      <template slot="status" slot-scope="e">
        <el-switch
          :value="e.row.Status == 1"
          active-text="提交"
          :inactive-text="e.row.Status == 0 ? '起草' : '撤回'"
          @change="(val) => setStatus(e.row, val)"
        />
      </template>
      <template slot="action" slot-scope="e">
        <el-button
          v-if="e.row.Status != 1 && e.row.IsEnd == false"
          size="mini"
          type="primary"
          title="填报"
          icon="el-icon-edit"
          circle
          @click="edit(e.row)"
        />
      </template>
    </w-table>
  </el-card>
</template>

<script>
import moment from 'moment'
import * as fillApi from '@/OA/api/userFill'
import { DictItemDic, EnumDic, UserFillStatus } from '@/OA/config/oaConfig'
export default {
  components: {},
  data() {
    return {
      DictItemDic,
      EnumDic,
      UserFillStatus,
      items: [],
      fillType: {},
      columns: [
        {
          key: 'Title',
          title: '标题',
          align: 'left',
          minWidth: 250
        },
        {
          key: 'FillType',
          title: '填报类型',
          align: 'center',
          slotName: 'fillType',
          width: 100
        },
        {
          key: 'FillInTime',
          title: '填报时段',
          align: 'center',
          slotName: 'fillInTime',
          minWidth: 200
        },
        {
          key: 'CreateEmp',
          title: '发布人',
          align: 'center',
          minWidth: 150
        },
        {
          key: 'Status',
          title: '填写状态',
          align: 'center',
          slotName: 'status',
          minWidth: 120
        },
        {
          key: 'fillTime',
          title: '提交时间',
          align: 'center',
          slotName: 'fillTime',
          minWidth: 120
        },
        {
          key: 'Action',
          title: '操作',
          align: 'left',
          fixed: 'right',
          width: '180px',
          slotName: 'action'
        }
      ],
      paging: {
        Size: 20,
        Index: 1,
        SortName: 'PublicTime',
        IsDesc: true,
        Total: 0
      },
      visible: false,
      id: null,
      queryParam: {
        CompanyId: null,
        QueryKey: null,
        FillType: null,
        Status: null,
        Begin: null,
        End: null
      }
    }
  },
  computed: {},
  mounted() {},
  methods: {
    moment,
    loadFillType(e) {
      this.fillType = e.items
      this.reset()
    },
    edit(row) {
      this.$router.push({ name: 'userFillEdit', params: { id: row.ItemId }})
    },
    async setStatus(row, isSubmit) {
      if (isSubmit) {
        await fillApi.Submit(row.ItemId)
      } else {
        await fillApi.Revoke(row.ItemId)
      }
      this.$message({
        type: 'success',
        message: '设置成功!'
      })
      row.Status = isSubmit ? 1 : 2
    },
    async load() {
      const res = await fillApi.Query(this.queryParam, this.paging)
      if (res.List) {
        const now = moment()
        res.List.forEach(a => {
          a.IsEnd = moment(a.EndDate) <= now
        })
        this.items = res.List
      } else {
        this.items = []
      }
      this.paging.Total = res.Count
    },
    reset() {
      this.queryParam.QueryKey = null
      this.queryParam.Status = null
      this.queryParam.FillType = null
      this.queryParam.Begin = null
      this.queryParam.End = null
      this.paging.Index = 1
      this.load()
    }
  }
}
</script>
